﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Model.Security;
using System.Collections.Generic;
using Cn.Edwon.Spring;
using Cn.Edwon.Biz;
using System.Text;
using Cn.Edwon.Web.Script;
using Cn.Cellula.Biz.Port;
using Cn.Cellula.Biz;

namespace Cn.Cellula.Website.Ws {
    public partial class _User : PageSupport {
        String Mode = "";
        String powerLevel = "";
        String roleKey = "";
        String roleLevelGt = "";
        String roleLevelLt = "";
        String roleLevelEGt = "";
        String roleLevelELt = "";
        IUserBiz biz = BizFactory.Get<IUserBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            Mode = Request.QueryString["mode"];
            powerLevel = Request.QueryString["p"];
            roleKey = Request.QueryString["r"];
            roleLevelGt = Request.QueryString["rgt"];
            roleLevelLt = Request.QueryString["rlt"];
            roleLevelEGt = Request.QueryString["regt"];
            roleLevelEGt = Request.QueryString["relt"];
            if (Mode == "rddl") {
                ResponseResult(LoadUserDdlSelector());
            } else if (Mode == "low") {
                ResponseResult(LoadLowerUser());
            } else {
                ResponseResult("[]");
            }
        }
        private void ResponseResult(String content) {
            Response.Clear();
            Response.Write(content);
        }

        private String LoadLowerUser() {
            if (base.Actor == null) {
                return "[]";
            } else {
                IList<VSysUserEO> list = biz.LoadLowerUser(base.Actor.ID, base.Actor.Power,(Request.QueryString["self"]=="1"));
                return JsUtil.ParseEOList2Array<VSysUserEO>(list, "UserKey,UserName,DeptName,Duty");
            }
        }

        private String LoadUserDdlSelector() {
            String hql = "from VSysUserEO eo ";
            hql += " where eo.UserKey<>'"+BizConst.Admin.KEY+"' ";
            if (powerLevel == "1") {//过滤权级，只列出比操作者角色等级低的人
                hql += " and eo.RoleLevel<=" + this.Power.RoleLevel;
            }
            if (!String.IsNullOrEmpty(roleKey)) {
                hql += " and eo.RoleKey='" + roleKey+"'";
            }
            if (!String.IsNullOrEmpty(roleLevelGt)) {
                hql += " and eo.RoleLevel>" + roleLevelGt+ "";
            }
            if (!String.IsNullOrEmpty(roleLevelLt)) {
                hql += " and eo.RoleLevel<" + roleLevelLt + "";
            }
            if (!String.IsNullOrEmpty(roleLevelEGt)) {
                hql += " and eo.RoleLevel>=" + roleLevelEGt + "";
            }
            if (!String.IsNullOrEmpty(roleLevelELt)) {
                hql += " and eo.RoleLevel<=" + roleLevelELt + "";
            }


            hql += " order by eo.UserName ";
            IList<VSysUserEO> list = biz.Find<VSysUserEO>(hql);
            return JsUtil.ParseEOList2Array<VSysUserEO>(list, "UserKey,UserName,DeptName");
        }

    }
}
